package form;

import database.DBO;
import widget.*;

import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by LLAP on 2017/12/25.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class DemandAdd extends Form {

    public DemandAdd() {
        super("求购/求租客户登记", 800, 600);
        this.setMaxWidth(800);
        JButton confirm = new JButton("Confirm");
        QueryTable table = new QueryTable();
        table.addRow().add(new TLabel("求购/求租客户登记"));
        table.addRow().add(new TTextField("name", "客户姓名", true));
        table.addRow().add(new TRadioGroup("gender", new Object[] {"男", "女"}, "性别", true));
        table.addRow().add(new TTextField("phone", "联系电话", true));
        table.addRow().add(new TLabel("求购/求租意向"));
        table.addRow().add(new TRadioGroup("demand", new Object[] {"求购", "求租"}, "意向", true));
        table.addRow().add(new TMultipleSelect("type", new Object[] {"平层", "复式", "跃式"}, "户型", true));
        table.addRow().add(new TMultipleSelect("decor", new Object[] {"精装", "简装", "毛坯"}, "装修", true));
        table.addRow().setHeight(100).add(
                new TMultipleSelect("direction", new Object[] {"东", "西", "南", "北"}, "房屋朝向", true)
        );
        table.addRow().add(new TTextField("area", "面积（平方米）", true));
        table.addRow().add(new TTextField("price", "底价（万元）", true));
        this.add("Center", table.pack());
        this.add("South", confirm);

        confirm.addActionListener(e -> {
            try {
                HashMap<String, Object> data = table.getData();
                //System.out.println(data);
                DBO db = new DBO();
                int cid = db.create("customer", new HashMap<String, Object>() {{
                    put("name", data.get("name"));
                    put("gender", data.get("gender"));
                    put("phone", data.get("phone"));
                }});
                int did = db.create("demand", new HashMap<String, Object>() {{
                    put("name", data.get("name"));
                    put("gender", data.get("gender"));
                    put("phone", data.get("phone"));
                    put("demand", data.get("demand"));
                    put("type", data.get("type").toString().replace("[", "").replace("]", ""));
                    put("decor", data.get("decor").toString().replace("[", "").replace("]", ""));
                    put("direction", data.get("direction").toString().replace("[", "").replace("]", ""));
                    put("area", data.get("area"));
                    put("price", data.get("price"));
                }});
                for (String i : (ArrayList<String>)data.get("decor")) {
                    db.create("demand_decor", new HashMap<String, Object>() {{
                        put("did", did);
                        put("decor", i);
                    }});
                }
                for (String i : (ArrayList<String>)data.get("type")) {
                    db.create("demand_type", new HashMap<String, Object>() {{
                        put("did", did);
                        put("type", i);
                    }});
                }
                for (String i : (ArrayList<String>)data.get("direction")) {
                    db.create("demand_direction", new HashMap<String, Object>() {{
                        put("did", did);
                        put("direction", i);
                    }});
                }
                JOptionPane.showMessageDialog(this, "添加成功");
                dispose();
            }
            catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
                ex.printStackTrace();
            }
        });
    }

    public static void main(String[] args) {
        new DemandAdd().setVisible(true);
    }
}
